package com.smart.translator.ui.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.smart.translator.R;
import com.smart.translator.data.model.Language;
import java.util.List;

public class HomeFragment extends Fragment {
    private HomeViewModel viewModel;
    private Spinner otherLangSpinner, myLangSpinner, micSpinner, voiceSpinner;
    private Button adBtn;
    private ImageButton qrBtn;
    private TextView versionText, timeLeftText, balanceText;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        otherLangSpinner = view.findViewById(R.id.otherLangSpinner);
        myLangSpinner = view.findViewById(R.id.myLangSpinner);
        micSpinner = view.findViewById(R.id.micSpinner);
        voiceSpinner = view.findViewById(R.id.voiceSpinner);
        adBtn = view.findViewById(R.id.adBtn);
        qrBtn = view.findViewById(R.id.qrBtn);
        versionText = view.findViewById(R.id.versionText);
        timeLeftText = view.findViewById(R.id.timeLeftText);
        balanceText = view.findViewById(R.id.balanceText);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        viewModel = new ViewModelProvider(this).get(HomeViewModel.class);
        // 语言选项
        viewModel.getOtherLangOptions().observe(getViewLifecycleOwner(), langs -> {
            ArrayAdapter<Language> adapter = new ArrayAdapter<>(requireContext(), android.R.layout.simple_spinner_item, langs);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            otherLangSpinner.setAdapter(adapter);
        });
        viewModel.getMyLangOptions().observe(getViewLifecycleOwner(), langs -> {
            ArrayAdapter<Language> adapter = new ArrayAdapter<>(requireContext(), android.R.layout.simple_spinner_item, langs);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            myLangSpinner.setAdapter(adapter);
        });
        viewModel.getOtherLangIndex().observe(getViewLifecycleOwner(), idx -> otherLangSpinner.setSelection(idx));
        viewModel.getMyLangIndex().observe(getViewLifecycleOwner(), idx -> myLangSpinner.setSelection(idx));
        otherLangSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) { viewModel.setOtherLangIndex(pos); }
            @Override public void onNothingSelected(AdapterView<?> parent) {}
        });
        myLangSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) { viewModel.setMyLangIndex(pos); }
            @Override public void onNothingSelected(AdapterView<?> parent) {}
        });
        // mic/voice
        viewModel.getMicOptions().observe(getViewLifecycleOwner(), opts -> {
            ArrayAdapter<String> adapter = new ArrayAdapter<>(requireContext(), android.R.layout.simple_spinner_item, opts);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            micSpinner.setAdapter(adapter);
        });
        viewModel.getVoiceOptions().observe(getViewLifecycleOwner(), opts -> {
            ArrayAdapter<String> adapter = new ArrayAdapter<>(requireContext(), android.R.layout.simple_spinner_item, opts);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            voiceSpinner.setAdapter(adapter);
        });
        viewModel.getMicIndex().observe(getViewLifecycleOwner(), idx -> micSpinner.setSelection(idx));
        viewModel.getVoiceIndex().observe(getViewLifecycleOwner(), idx -> voiceSpinner.setSelection(idx));
        micSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) { viewModel.setMicIndex(pos); }
            @Override public void onNothingSelected(AdapterView<?> parent) {}
        });
        voiceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) { viewModel.setVoiceIndex(pos); }
            @Override public void onNothingSelected(AdapterView<?> parent) {}
        });
        // 静态信息
        viewModel.getVersion().observe(getViewLifecycleOwner(), t -> versionText.setText("版本：" + t));
        viewModel.getTimeLeft().observe(getViewLifecycleOwner(), t -> timeLeftText.setText("剩余时长：" + t));
        viewModel.getBalance().observe(getViewLifecycleOwner(), t -> balanceText.setText("余额：" + t));
        // 二维码、广告
        qrBtn.setOnClickListener(v -> viewModel.onQrClick());
        adBtn.setOnClickListener(v -> viewModel.onAdClick());
        // 错误提示
        viewModel.getError().observe(getViewLifecycleOwner(), err -> {
            if (!TextUtils.isEmpty(err)) {
                Toast.makeText(requireContext(), err, Toast.LENGTH_SHORT).show();
            }
        });
    }
} 